from OpenGL.arrays import vbo
from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
from numpy import array
import sys, math, os

##points = [1,2,3,4,5,4,6,7,6,8,9,10,9,11,12,13]
points = [[1,0], # 1
          [2,2], # 2
          [3,0], # 3 
          [4,2], # 4
          [5,0], # 5
          [4,2], # 4
          [6,2], # 6
          [5,4], # 7
          [6,2], # 6
          [7,4], # 8
          [8,2], # 9
          [9,4], # 10
          [8,2], # 9
          [10,2],# 11
          [9,0], # 12
          [11,0],# 13
          ]
         
vbo = vbo.VBO(array(points, 'f')) # vbo an Graka schicken

def init(width, height):
   """ Initialize an OpenGL windown and rendering context"""
   glClearColor(1, 1, 1, 1)         # background color
   glMatrixMode(GL_PROJECTION)      # switch to projection matrix
   glLoadIdentity()                 # set to 1
   gluPerspective(45.,
                  float(width)/height,
                  1,
                  100
                  )
                  
##   gluOrtho2D(-1, 15,               # links, rechts clipping planes
##              -1, 15)               # unten, oben  clipping planes
##   
   glMatrixMode(GL_MODELVIEW)       # switch to modelview matrix
   
   
def display():
   """Rendering einstellungen"""
   glClear(GL_COLOR_BUFFER_BIT ) # resettet angegebenen Buffer
   glColor3f(0,0,0)
   gluLookAt(3,1,10,
             3,1,-1,
             0,1,0
             )
   glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
   # Render Vertex Buffer Object
   vbo.bind()
   glVertexPointerf(vbo)
   glEnableClientState(GL_VERTEX_ARRAY)
   glDrawArrays(GL_TRIANGLE_STRIP, 0, 16) # !! Anzahl Arrayelemente mit doppelten Kanten beachten
   vbo.unbind()
   glDisableClientState(GL_VERTEX_ARRAY)
##   glFlush() # Flush commands
   glutSwapBuffers()
   
 
def reshape():
   pass
   
def main():
   width, height = 500, 500
   glutInit(sys.argv)
   glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB )   
   glutInitWindowSize(width, height)
   glutCreateWindow("Test OPENGL Fensterchen")
   glutDisplayFunc(display)     #register display function

   
   # Hack for Mac OS X
#   cwd = os.getcwd()
#   glutInit(sys.argv)
#   os.chdir(cwd)

   init(width, height)#initialize OpenGL state and opengl context
   glutMainLoop()   #start even processing

if __name__ == "__main__":
   main()
